Imports System.Data

Public Class View
    Dim Con As Controller
    Private Sub View_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Con = New Controller
    End Sub
    Private Sub ctrl_Run_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ctrl_Run.Click

        Dim Demand As Long = CLng(ctrl_Demand.Text)
        Dim MaxCost As Single = CSng(ctrl_Cost.Text)

        ResultsList.Items.Clear()
        StatusLabel.Text = ""
        Con = New Controller

        If MaxCost = 0 Then
            MaxQuality(Demand)
        Else
            CostConstrained(Demand, MaxCost)
        End If
    End Sub
    Private Sub ctrl_WorkingSuppliers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ctrl_WorkingSuppliers.Click
        Dim Window As New View_Suppliers
        Window.Show()
        Window.DisplaySuppliers(Con.WorkTable)
    End Sub
    Private Sub ctrl_AllSuppliers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ctrl_AllSuppliers.Click
        Dim Window As New View_Suppliers
        Window.Show()
        Window.DisplaySuppliers(Con.SupplierTable)
    End Sub
    Private Sub MaxQuality(ByVal Demand As Long)
        'Calculate the maximum quality without regard to price
        If Con.MaxQualityCalculation(Demand) Then
            DisplayResults()
        Else
            StatusLabel.Text = "Error: The specified criteria cannot be met"
        End If
    End Sub
    Private Sub CostConstrained(ByVal Demand As Long, ByVal MaxCost As Single)
        'Calculate the maximum quality without regard to price
        If Con.CostConstrainedCalculation(Demand, MaxCost) Then
            DisplayResults()
        Else
            StatusLabel.Text = "Error: The specified criteria cannot be met"
        End If
    End Sub
    Private Sub DisplayResults()
        Dim ListRow As ListViewItem
        Dim View As DataView
        Dim Index As Long = 0
        Dim TierNumber As Long = 0
        Dim TierQuality As Single = 0.0
        Dim TierCost As Single = 0.0
        Dim TotalCost As Single = 0.0
        Dim TotalQuality As Single = 1.0

        View = New DataView(Con.TierTable)
        View.Sort = "[Tier_No] ASC"

        For Index = 0 To (View.Count - 1)
            TierNumber = View.Item(Index).Item(0)
            TierQuality = View.Item(Index).Item("Tier_Quality")
            TierCost = View.Item(Index).Item("Tier_Cost")

            'Add a row to the results table
            ResultsList.BeginUpdate()
            ListRow = New ListViewItem("Tier " & TierNumber.ToString())
            ListRow.SubItems.Add(Math.Round(TierQuality, 3).ToString())
            ListRow.SubItems.Add(Math.Round(TierCost, 2).ToString())
            ResultsList.Items.Add(ListRow)
            ResultsList.EndUpdate()

            'calculate the effect of this tier of total quality and cost
            TotalQuality *= TierQuality
            TotalCost += TierCost
        Next

        'Add the "total" row
        ResultsList.BeginUpdate()
        ListRow = New ListViewItem("SCQ")
        ListRow.SubItems.Add(Math.Round(TotalQuality, 3).ToString())
        ListRow.SubItems.Add(Math.Round(TotalCost, 2).ToString())
        ResultsList.Items.Add(ListRow)
        ResultsList.EndUpdate()

    End Sub
End Class
